package com.cims.dao.grave;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.cims.common.exception.DaoException;
import com.cims.common.vo.Grave;
import com.cims.common.vo.Land;
import com.cims.common.vo.OrderItem;
import com.cims.common.vo.Task;
import com.cims.dao.hibernate.HibernateUtil;

public class GraveDAOImpl implements GraveDAO {

	@Override
	public List<Grave> getListGrave() throws DaoException {
		return HibernateUtil.selectAll(Grave.class);
	}

	@Override
	public Grave findByGraveId(int graveId) throws DaoException {
		return HibernateUtil.findByID(Grave.class, graveId);
	}

	@Override
	public boolean update(Grave grave) throws DaoException {
		return HibernateUtil.updateVo(grave);
	}

	@Override
	public boolean insert(Grave grave) throws DaoException {
		return HibernateUtil.insertVo(grave);
	}

	@Override
	public List<Land> getListLand() throws DaoException {
		return HibernateUtil.selectAll(Land.class);
	}

	@Override
	public List<OrderItem> getListGraveOfCustomer(int customerId)
			throws DaoException {
		List<OrderItem> lstVo = null;
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = null;

		try {
			session = factory.openSession();

			Query query = session
					.createQuery("from OrderItem as O where O.customer.customerId = :customerId and (O.item.itemId in (select G.item.itemId from Grave as G)) and (O.gravePurchased = null OR O.gravePurchased.id < 2)");
			query.setParameter("customerId", customerId);
			lstVo = query.list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}

		return lstVo;
	}

}
